import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_plugin_a/flutter_plugin_a.dart';
import 'package:flutter_plugin_a/native_call_flutter.dart';
import 'package:flutter_plugin_a/native_send_msg_to_fluuter.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _platformVersion = 'Unknown';
  int _result = 0;
  Map _mapInfo = {};
  List _listInfo = [];

  @override
  void initState() {
    super.initState();
    initPlatformState();
    initResult();
    initMapInfo();
    initListInfo();

    NativeCallFlutter();
    NativeSendMsgToFlutter.initMessageHandler();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion = await FlutterPlugin_a.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  Future<void> initResult() async {
    int result = 0;
    try {
      result = await FlutterPlugin_a.addInt(2, 3);
    } on PlatformException {
      result = -1;
    }

    if (!mounted) return;

    setState(() {
      _result = result;
    });
  }

  Future<void> initMapInfo() async {
    Map mapInfo = {};
    try {
      mapInfo = await FlutterPlugin_a.getMapInfo();
    } on PlatformException {
      mapInfo = {};
    }

    if (!mounted) return;

    setState(() {
      _mapInfo = mapInfo;
    });
  }

  Future<void> initListInfo() async {
    List listInfo = [];
    try {
      listInfo = await FlutterPlugin_a.getListInfo();
    } on PlatformException {
      listInfo = [];
    }

    if (!mounted) return;

    setState(() {
      _listInfo = listInfo;
    });
  }



  // page
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
//          child: Text('Running on: $_platformVersion\n'),
          child: Column(
            children: <Widget>[
              Text('Running on: $_platformVersion\n'),
              Text('2 + 3 = $_result\n'),
              Text('dic info = $_mapInfo\n'),
              Text('array info = $_listInfo\n')
            ],
          ),
        ),
      ),
    );
  }

}
